package list2;

import java.util.Arrays;

public class SeqList<E> implements List<E> {
    public int flag = 0;
    private Object[] arr;
    public int usedSize;
    private static final int DEFAULT_CAPACITY = 10;
    public SeqList() {
        arr = new Object[DEFAULT_CAPACITY];
    }
    @Override
    public boolean isFull() {
        return arr.length == usedSize;
    }

    private void grow() {
        this.arr = Arrays.copyOf(this.arr, 2*this.arr.length);
    }
    @Override
    public void add(E data) {
        if (isFull()) {
            grow();
        }
        this.arr[usedSize] = data;
        usedSize++;
    }
    private void isIllegle(int pos) throws PosIllegle {
        if (pos < 0 || pos > this.usedSize) {
            throw new PosIllegle("pos位置不合法！！！");
        }
    }
    private void isIllegle2(int pos) throws PosIllegle {
        if (pos < 0 || pos >= this.usedSize) {
            throw new PosIllegle("pos位置不合法！！！");
        }
    }
    @Override
    public void add(int pos, E data) {
        if (isFull()) {
            grow();
        }
        try {
            isIllegle(pos);
            for (int i = this.usedSize-1; i >= pos ; i--) {
                this.arr[i+1] = this.arr[i];
            }
            this.arr[pos] = data;
            usedSize++;
        } catch (PosIllegle e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(E toFind) throws NonNum {
        for (int i = 0; i < usedSize; i++) {
            if (this.arr[i].equals(toFind)) {
                return true;
            }
        }
        throw new NonNum("没有这个数据!!!");
    }

    @Override
    public int indexOf(E toFind) {
        try {
            contains(toFind);
            for (int i = 0; i < usedSize; i++) {
                if (this.arr[i].equals(toFind)) {
                    return i;
                }
            }
        } catch (NonNum e) {
            e.printStackTrace();
        }
        return -1;
    }
    private void checkEmpty() {
        if (usedSize == 0) {
            throw new Empty("数组为空");
        }
    }
    @Override
    public E get(int pos) {
        try {
            checkEmpty();
            isIllegle2(pos);
            return (E)arr[pos];
        } catch (PosIllegle e) {
            e.printStackTrace();
        } catch (Empty e) {
            e.printStackTrace();
        }
        return (E)null;
    }

    @Override
    public void set(int pos, E value) {
        try {
            isIllegle(pos);
            this.arr[pos] = value;
        } catch (PosIllegle e) {
            e.printStackTrace();
        }
    }

    @Override
    public void remove(E toRemove){
        try {
            checkEmpty();
            contains(toRemove);
            int index = indexOf(toRemove);
            for (int i = index; i < usedSize-1; i++) {
                this.arr[i] = this.arr[i+1];
            }
            usedSize--;
        } catch (NonNum e){
            e.printStackTrace();
        } catch (Empty e) {
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        for (int i = 0; i < this.usedSize; i++) {
            arr[i] = null;
        }
        usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}
